package com.maserb.monitor.common;

import java.util.ArrayList;
        import java.util.List;

import cn.jmessage.api.message.MessageClient;
import cn.jmessage.api.message.MessageListResult;
import cn.jmessage.api.message.MessageResult;
import com.maserb.monitor.model.JpushMessageModel;
import com.maserb.monitor.model.JpushMsgBodyModel;
import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;
        import cn.jiguang.common.ClientConfig;
        import cn.jiguang.common.ServiceHelper;
        import cn.jiguang.common.connection.ApacheHttpClient;
        import cn.jiguang.common.resp.APIConnectionException;
        import cn.jiguang.common.resp.APIRequestException;
        import cn.jiguang.common.resp.ResponseWrapper;
        import cn.jmessage.api.JMessageClient;
        import cn.jmessage.api.common.model.RegisterInfo;
        import cn.jmessage.api.common.model.RegisterPayload;
        import cn.jmessage.api.user.UserClient;

/**
 * @author
 * @date 2018年7月9日
 */
public class JpushImClientUtil {
    protected static final Logger LOG = LoggerFactory.getLogger(JpushImClientUtil.class);
    private static final String appKey = "5badead56a37c089dbe78a85";
    private static final String masterSecret = "a4f4e5c14194b5fd7a97c6f8";

    //方法一:利用ApacheHttpClient代替JMessageClient进行用户的注册
    public static void regPP(String username,String password) {
        JMessageClient client = new JMessageClient(appKey, masterSecret);
        String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
        ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, ClientConfig.getInstance());
        client.setHttpClient(httpClient);
        try {
            List<RegisterInfo> users = new ArrayList<RegisterInfo>();
            RegisterInfo user = RegisterInfo.newBuilder()
                    .setUsername(username)
                    .setPassword(password)
                    .build();
            users.add(user);
            RegisterInfo[] regUsers = new RegisterInfo[users.size()];

            String res = client.registerUsers(users.toArray(regUsers));
            LOG.info(res);
        } catch (APIConnectionException e) {
            LOG.error("连接错误。请稍后重试。 ", e);
        } catch (APIRequestException e) {
            LOG.error("JPush服务器的错误响应。请检查并改正。", e);
            LOG.info("网络状态: " + e.getStatus());
            LOG.info("错误信息: " + e.getMessage());
        }
    }

    //方法二 ：利用UserClient来进行用户的注册
    public static void addUser(String username,String password,String nickname) {
        UserClient client = new UserClient(appKey, masterSecret);

        try {
            List<RegisterInfo> users = new ArrayList<RegisterInfo>();
            RegisterInfo user = RegisterInfo.newBuilder()
                    .setUsername(username)
                    .setPassword(password)
                    .setNickname(nickname)
                    .build();
            users.add(user);
            RegisterInfo[] regUsers = new RegisterInfo[users.size()];

            RegisterPayload payload = RegisterPayload.newBuilder()
                    .addUsers(users.toArray(regUsers)).build();
            ResponseWrapper registerUsers = client.registerUsers(payload);

            System.out.println(registerUsers);
            // LOG.info(res);
        } catch (APIConnectionException e) {
            LOG.error("连接错误。请稍后重试。 ", e);
        } catch (APIRequestException e) {
            LOG.error("JPush服务器的错误响应。请检查并改正。", e);
            LOG.info("网络状态: " + e.getStatus());
            LOG.info("错误信息: " + e.getMessage());
        }
    }

    /*public static MessageListResult getUserMessages(String username, int count, String begin_time, String end_time){
        MessageClient client = new MessageClient(appKey,masterSecret);
        MessageListResult result = null;
        try{
            result = client.getUserMessages(username,count,begin_time,end_time);
            return result;
        }catch (APIConnectionException e){
            LOG.error("连接错误。请稍后重试。 ", e);
        }catch (APIRequestException e) {
            LOG.error("JPush服务器的错误响应。请检查并改正。", e);
            LOG.info("网络状态: " + e.getStatus());
            LOG.info("错误信息: " + e.getMessage());
        }
        return result;
    }*/

    public static List<JpushMessageModel> getUserMessages(String username, int count, String begin_time, String end_time){
        MessageClient client = new MessageClient(appKey,masterSecret);
        MessageListResult result = null;
        List<JpushMessageModel> modelList = new ArrayList<JpushMessageModel>();
        try{
            result = client.getUserMessages(username,count,begin_time,end_time);

            if(null != result && result.getMessages().length>0){
                for(MessageResult messageResult:result.getMessages()){
                    JpushMessageModel model = new JpushMessageModel();
                    JpushMsgBodyModel bodyModel = new JpushMsgBodyModel();
                        /*BeanUtils.copyProperties(model, messageResult);
                        BeanUtils.copyProperties(bodyModel, messageResult.getMsgBody());*/
                    model.setVersion(messageResult.getVersion());
                    model.setCreate_time(messageResult.getCreateTime());
                    model.setCreateTime(CreateTimeUtils.getDateByM(messageResult.getCreateTime()));
                    model.setFrom_id(messageResult.getFromId());
                    model.setFrom_name(messageResult.getFromName());
                    model.setFrom_type(messageResult.getFromType());
                    model.setFrom_platform(messageResult.getFromPlatform());
                    model.setMsg_ctime(messageResult.getMsgCtime());
                    model.setMsgTime(CreateTimeUtils.getDateByM(messageResult.getMsgCtime()));
                    model.setMsg_level(messageResult.getMsgLevel());
                    model.setMsg_type(messageResult.getMsgType());
                    model.setMsgid(messageResult.getMsgId());
                    model.setTarget_id(messageResult.getTargetId());
                    model.setTarget_name(messageResult.getTargetName());
                    model.setTarget_type(messageResult.getTargetType());
                    model.setNo_notification(messageResult.getNoNotification());
                    model.setNo_offline(messageResult.getNoOffline());

                    bodyModel.setExtras(messageResult.getMsgBody().getExtras());
                    bodyModel.setText(messageResult.getMsgBody().getText());
                    bodyModel.setFormat(messageResult.getMsgBody().getFormat());
                    bodyModel.setFsize(messageResult.getMsgBody().getFsize());
                    bodyModel.setMedia_crc32(messageResult.getMsgBody().getMediaCrc32());
                    bodyModel.setMedia_id(messageResult.getMsgBody().getMediaId());
                    bodyModel.setWidth(messageResult.getMsgBody().getWidth());
                    bodyModel.setHeight(messageResult.getMsgBody().getHeight());
                    model.setMsgBody(bodyModel);
                    modelList.add(model);
                }
            }
            return modelList;
        }catch (APIConnectionException e){
            LOG.error("连接错误。请稍后重试。 ", e);
        }catch (APIRequestException e) {
            LOG.error("JPush服务器的错误响应。请检查并改正。", e);
            LOG.info("网络状态: " + e.getStatus());
            LOG.info("错误信息: " + e.getMessage());
        }
        return modelList;
    }


}
